﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.Master {
    public partial class AdminManager : PageBase {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.aspDataSource.IsEditing && !this.aspDataSource.IsNewRowEditing) {
                DataBinds();
            }
        }

        public void DataBinds() {

            var dataList = TrainingsApp.Logic.TS_User_Logic.TS_User_ListTopByUserType((int)UserType.Admin);
            if (!string.IsNullOrEmpty(this.txtUserName.Text.Trim())) {
                dataList.TS_User_List = dataList.TS_User_List.Where(u => u.UserName.Contains(this.txtUserName.Text)).ToList();
            }
            this.aspDataSource.DataSource = dataList.TS_User_List;
            this.aspDataSource.DataBind();
        }

        protected void aspDataSource_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
            DataBinds();
        }

        protected void aspDataSource_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            long id = Convert.ToInt64(e.Keys["Idx"]);
            TS_User_Logic.TS_User_Delete(id);
        }

        protected void aspDataSource_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e) {
            e.ExceptionHandled = true;
            DataBinds();
        }

        protected void aspDataSource_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
            string userName = e.NewValues["UserName"].ToString();
            string Name = e.NewValues["Name"].ToString();
            string IdentityCard = e.NewValues["IdentityCard"].ToString();
            string Mobile = e.NewValues["Mobile"].ToString();
            string Email = e.NewValues["Email"].ToString();
            string Password = e.NewValues["Password"].ToString();
            TS_User_Entity entity = new TS_User_Entity();

            entity.UserName = userName;
            entity.Name = Name;
            entity.IdentityCard = IdentityCard;
            entity.Mobile = Mobile;
            entity.Password = Password;
            entity.Email = Email;
            entity.UserType = (int)UserType.Admin;
            TrainingsApp.Logic.TS_User_Logic.TS_User_Add(entity);
        }

        protected void aspDataSource_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e) {
            e.ExceptionHandled = true;
        }

        protected void aspDataSource_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) {
            long id = Convert.ToInt64(e.Keys["Idx"]);

            string userName = e.NewValues["UserName"].ToString();
            string Name = e.NewValues["Name"].ToString();
            string IdentityCard = e.NewValues["IdentityCard"].ToString();
            string Mobile = e.NewValues["Mobile"].ToString();
            string Email = e.NewValues["Email"].ToString();
            string Password = e.NewValues["Password"].ToString();
            TS_User_Entity entity = new TS_User_Entity();
            entity.Idx = id;
            entity.UserName = userName;
            entity.Password = Password;
            entity.Name = Name;
            entity.IdentityCard = IdentityCard;
            entity.Mobile = Mobile;
            entity.Email = Email;
            entity.UserType = (int)UserType.Admin;
            TrainingsApp.Logic.TS_User_Logic.TS_User_Update(entity);

        }
    }
}